﻿using System;
using System.IO;

namespace UwaySoft.EsheetServer.BizLogic
{
    public class FileHelper
    {
        public static bool FileExists(string fullName)
        {
            return File.Exists(fullName);
        }

        public static string GetFileExtesion(string fullName)
        {
            return Path.GetExtension(fullName);
        }

        public static string CreateFile(string fullName)
        {
            var fileInfo = new FileInfo(fullName);

            if (!fileInfo.Exists)
                fileInfo.Create();

            return fileInfo.FullName;
        }

        public static void CreateFile(string fullName, string content)
        {
            if(content == null)
                throw new ArgumentNullException("content");

            var fileInfo = new FileInfo(fullName);

            if(fileInfo.Exists)
                return;

            fileInfo.Create();
            fileInfo.CreateText().WriteLine(content);
        }

        public static bool Delete(string fullName)
        {
            if (!File.Exists(fullName))
                return false;

            File.Delete(fullName);
            return true;
        }

        public static void WriteFile(string fullName, string text)
        {
            using(var fileStream = new FileStream(fullName, FileMode.Append))
            {
                var arrByte = new byte[text.Length];

                for (var i = 0; i < arrByte.Length; i++)
                    arrByte[i] = (byte) text[i];

                fileStream.Write(arrByte,0,arrByte.Length);
            }
        }
    }
}
